package tech.eyao.hs60x.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import tech.eyao.hs60x.annotation.Admin;
import tech.eyao.hs60x.common.ReturnCode;
import tech.eyao.hs60x.dto.AddressBookDTO;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.dto.FriendDTO;
import tech.eyao.hs60x.dto.ShakeDTO;
import tech.eyao.hs60x.service.AccountRelationService;
import tech.eyao.hs60x.service.ShakeService;
import tech.eyao.hs60x.utils.TokenUtils;

/**
 * 通讯录
 * 
 * @author hejian
 * @date 2017年12月28日 下午1:45:45
 */
@Api("好友相关")
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AccountRelationService accountRelationService;
    @Autowired
    private ShakeService           shakeService;

    /**
     * 通讯录查询
     * 
     * @return
     */
    @PostMapping(path = "/findFriends.do")
    @ApiOperation(value = "通讯录查询", response = AddressBookDTO.class)
    public BaseResult findFriends(@ApiParam("手表ID") Long watchId) {
        return accountRelationService.findFriends(TokenUtils.getUserId(), watchId);
    }

    /**
     * 好友列表，微聊 TODO 设备好友+通讯录
     * 
     * @return
     */
    @PostMapping(path = "/allFriends.do")
    @ApiOperation(value = "群聊好友列表(设备好友+通讯录)", response = FriendDTO.class)
    public BaseResult allFriends(Long watchId) {
        return accountRelationService.findAllFriends(TokenUtils.getUserId(), watchId);
    }

    /**
     * 手表端使用 摇一摇
     * 
     * @return
     */
    @PostMapping("/shake.do")
    @ApiOperation(value = "摇一摇-手表端", response = ShakeDTO.class)
    public BaseResult shake(double longitude, double latitude) {
        BaseResult baseResult = new BaseResult(ReturnCode.C_SUCCESS_CODE, ReturnCode.SUCCESS_CODE.getDesc());
        baseResult.data = shakeService.shake(TokenUtils.getUserId(), longitude, latitude);
        return baseResult;
    }

    @PostMapping("/addFriend.do")
    @ApiOperation("双向加好友-手表端")
    public BaseResult addFriend(@ApiParam("手表好友账户ID") long friendWatchId, @ApiParam("好友昵称") String friendName) {
        return shakeService.addFriend(TokenUtils.getUserId(), friendWatchId, friendName);
    }

    @Admin
    @PostMapping("/delFriend.do")
    @ApiOperation("删除好友-手机端")
    public BaseResult deleteFriend(@ApiParam("好友账户ID") long accountId, @ApiParam("手表账户ID") long watchId) {
        return accountRelationService.delete(TokenUtils.getUserId(), accountId, watchId);
    }

    @Admin
    @PostMapping("/addAddressBook.do")
    @ApiOperation("添加手表联系人-手机端")
    public BaseResult addAddressBook(@ApiParam("手表好友账户ID") long watchId, @ApiParam("添加联系人头像") String avatarUrl,
                                     @ApiParam("添加联系人昵称") String nickName, @ApiParam("添加联系人手机号码") String phone) {
        return accountRelationService.addAddressBook(TokenUtils.getUserId(), watchId, avatarUrl, nickName, phone);
    }

    @Admin
    @PostMapping("/modifyAddressBook.do")
    @ApiOperation("修改手表联系人-手机端")
    public BaseResult modifyAddressBook(@ApiParam("手表好友账户ID") long watchId, @ApiParam("联系人历史手机号") String historyPhone,
                                        @ApiParam("联系人头像") String avatarUrl, @ApiParam("联系人昵称") String nickName,
                                        @ApiParam("联系人手机号码") String phone) {
        return accountRelationService.modifyAddressBook(TokenUtils.getUserId(), watchId, historyPhone, avatarUrl,
                nickName, phone);
    }
}
